from awvs.base import Base


class Scan(Base):
    @property
    def __scan_fields(self):
        return [
            'target.address',
            'target_id',
            'scan_id',
            'progress',
            'start_date',
            'scan_session_id',
            'current_session.status',
            'current_session.severity_counts.high'
        ]

    @property
    def scans(self):
        api = f'{self.url}/scans'
        r = self.session.get(api)
        result = self.scroll_pages(r, api, 'scans')
        scans = Base.parse_fields(result, self.__scan_fields)
        return scans

    def start_scan(self, target_id, profile_id='11111111-1111-1111-1111-111111111111'):
        """
        官方定义的扫描策略:
            11111111-1111-1111-1111-111111111112    High Risk Vulnerabilities
            11111111-1111-1111-1111-111111111115    Weak Passwords
            11111111-1111-1111-1111-111111111117    Crawl Only
            11111111-1111-1111-1111-111111111116    Cross-site Scripting Vulnerabilities
            11111111-1111-1111-1111-111111111113    SQL Injection Vulnerabilities
            11111111-1111-1111-1111-111111111118    quick_profile_2 0   {"wvs": {"profile": "continuous_quick"}}
            11111111-1111-1111-1111-111111111114    quick_profile_1 0   {"wvs": {"profile": "continuous_full"}}
            11111111-1111-1111-1111-111111111111    Full Scan   1   {"wvs": {"profile": "Default"}}
        """
        api = f'{self.url}/scans'
        data = {
            "target_id": target_id,
            "profile_id": profile_id,
            "schedule":
                {
                    "disable": False,
                    "start_date": None,
                    "time_sensitive": False
                }
        }
        try:
            response = self.session.post(api, json=data)
            scan_id = response.headers['Location'][14:]
            return scan_id
        except Exception as e:
            print(e)
            return ''